Add Static Module Support#3392
Conversation
Add a build option to compile the Lua scripting engine as a static module and wire the server to load it directly at startup when enabled. The module load path now resolves on-load and on-unload entry points from the main binary, and the module lifecycle keeps those callbacks so unload works without a shared library handle. The Lua module build was updated to support both static and shared variants, with the static path exporting visible wrapper symbols and linking the server with the module archive. While touching the Lua code, a few internal symbols were renamed for consistency and the monotonic time helper was clarified. * CMake * Makefile * Lua scripting module * Core module loading **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## unstable #3392 +/- ##
============================================
- Coverage 76.45% 76.39% -0.07%
============================================
Files 159 159
Lines 79840 79879 +39
============================================
- Hits 61042 61022 -20
- Misses 18798 18857 +59
🚀 New features to boost your workflow:
|
|
No deep dive yet. I think we need to clarify requirements before proceeding with a deep dive. The original change aimed to:
For the 9.1 release, we must decide:
|
|
Core team discussion:
@valkey-io/core-team and @rjd15372 specifically in case you have some thoughts. See #2858 (comment) for some additional discussion. |
When a scripting engine is unregistered, its cached eval scripts are now removed as well. This prevents the eval cache from holding dangling references to the engine after it has been unloaded. The eval subsystem now exposes a helper for deleting all scripts owned by a specific engine, updates the eval cache bookkeeping as entries are removed, and logs how many scripts were cleaned up during engine unregistration. * scripting_engine * eval **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
Update the Lua module to use the engine-provided monotonic clock helper when built with STATIC_LUA, while keeping the module's local implementation for non-static builds. This avoids duplicate timing implementations and keeps elapsed-time handling consistent across build configurations. * Lua module * Static build integration **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
The server startup path now only auto-loads Lua at runtime for the static build. Consolidate Lua scripting engine build configuration around a single BUILD_LUA value with explicit static, module, and no modes. This removes the separate BUILD_STATIC_LUA toggle and updates the build system to derive compile definitions, link behavior, and module packaging from the selected mode. * Build system * Server startup * Lua module packaging **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
* Build system **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
|
@eifrah-aws please check the CI failures |
Adjust the module API hook test to inspect a larger tail of the replica log when verifying the shutdown event. This makes the assertion less brittle and better aligned with the amount of output produced during shutdown. **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
The extra log line caused an error to some of the TCL tests. Signed-off-by: Eran Ifrah <eifrah@amazon.com>
ranshid
left a comment
There was a problem hiding this comment.
Overall looks very good!
One high level comment I have is the lack of good testing. Can we place some tests that verify statoc AND dynamic lua are not breaking to load/unload and that the engine can start without a lua engine (also when eval/evalsha are being generated)?
In addition, removed duplicate code. Minor fix in the Makefile: settings LUA_MODULE_NAME to its correct name. Signed-off-by: Eran Ifrah <eifrah@amazon.com>
Signed-off-by: Eran Ifrah <eifrah@amazon.com>
zuiderkwast
left a comment
There was a problem hiding this comment.
Looks good in general, but it seems the visibility attributes things are not finalized?
Signed-off-by: Eran Ifrah <eifrah@amazon.com>
Update the documentation comments for the static module loading helpers to better match Valkey's standard. * API docs **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
I was just referring to the open comments, about symbol collisions or using |
- Removed __attribute__((visibility("default")))
- Added comments for the LUA_MODULE_VISIBILITY macro.
**Generated by CodeLite**
Signed-off-by: Eran Ifrah <eifrah@amazon.com>
I removed un-needed code and improved comments. I also merged with latest upstream |
ranshid
left a comment
There was a problem hiding this comment.
almost perfect. some small comments
The module system and Lua integration are updated to enhance reliability and stability, particularly during module loading and unloading. Key changes include: * Implementing conditional compilation for Lua module entry points to support static linking gracefully. * Hardening module unloading logic in `module.c` by adding checks before calling `dlclose` and improving error logging if the unload function fails to load. * Removing the redundant `freeCachedScriptsForEngine` function in the evaluation subsystem. * use `__attribute__((weak))` instead of `__attribute((weak))` in `src/modules/lua/script_lua.c`. * Module Management * Lua Integration * Script Evaluation **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
Signed-off-by: Eran Ifrah <eifrah@amazon.com>
Extract the common module initialization logic from `moduleLoad` and `moduleLoadStatic` into a new helper function `moduleInitPostOnLoadResolved`. This function handles the shared workflow of invoking the onload callback, registering the module, performing post-load validation, and firing server events. The refactoring eliminates approximately 70 lines of duplicated code while preserving all existing behavior. The helper function accepts parameters that control static vs. dynamic module handling, including whether to close the dlopen handle and how to format log messages. Both `moduleLoad` and `moduleLoadStatic` now focus on their specific responsibilities (dlopen/symbol resolution) and delegate the common initialization sequence to the shared helper. * Module loading infrastructure * Code deduplication **Generated by CodeLite** Signed-off-by: Eran Ifrah <eifrah@amazon.com>
- Move `evalRemoveScriptsOfEngine` code to a separate PR - Renamed `lua_scripts_flags_def` -> `lua_scripts_flags` with `static` Signed-off-by: Eran Ifrah <eifrah@amazon.com>
When building static lua, use: - `getMonotonicUs()` - `elapsedUs` - `elapsedMs` from the engine's `monotonic.h` header. Signed-off-by: Eran Ifrah <eifrah@amazon.com>
madolson
left a comment
There was a problem hiding this comment.
Sorry for the delay. The tests all seem unrelated, but lgtm
Thanks, for the review. I have updated the branch to the latest unstable. |
|
Forgot to update README.md with the build options? |
Add a build option to compile the Lua scripting engine as a static module and wire the server to load it directly at startup when enabled. The module load path now resolves on-load and on-unload entry points from the main binary, and the module lifecycle keeps those callbacks so unload works without a shared library handle. The Lua module build was updated to support both static and shared variants, with the static path exporting visible wrapper symbols and linking the server with the module archive. While touching the Lua code, a few internal symbols were renamed for consistency and the monotonic time helper was clarified. Note that this PR addresses the LUA module, but it can be applied to other "core" modules (like: Bloom, Json, Search and others). With this change, it will be easier to ship Valkey bundle with modules. Areas touched: * CMake * Makefile * Lua scripting module * Core module loading **Generated by CodeLite** --------- Signed-off-by: Eran Ifrah <eifrah@amazon.com>
Add a build option to compile the Lua scripting engine as a static module and wire the server to load it directly at startup when enabled. The module load path now resolves on-load and on-unload entry points from the main binary, and the module lifecycle keeps those callbacks so unload works without a shared library handle.
The Lua module build was updated to support both static and shared variants, with the static path exporting visible wrapper symbols and linking the server with the module archive. While touching the Lua code, a few internal symbols were renamed for consistency and the monotonic time helper was clarified.
Note that this PR addresses the LUA module, but it can be applied to other "core" modules (like: Bloom, Json, Search and others). With this change, it will be easier to ship Valkey bundle with modules.
Areas touched:
Generated by CodeLite